//
//  SegmentLineIndicatorDataSource.swift
//  PageNavigator
//
//  Created by 张奇 on 2025/8/15.
//

import UIKit



public class SegmentLineIndicatorDataSource: SegmentIndicatorDataSource {
    
    public var width: CGFloat = VIEW_AUTOMATIC_DIMENSION
    
    /// 指示器的宽度增量。比如需求是指示器宽度比cell宽度多10point。就可以将该属性赋值为10。
    /// 最终指示器的宽度=width+padding
    public var padding: CGFloat = 0
    
    public var height: CGFloat = 2
    /// 默认VIEW_AUTOMATIC_DIMENSION （等于indicatorHeight/2）。
    public var cornerRadius: CGFloat = VIEW_AUTOMATIC_DIMENSION
    /// 指示器的颜色
    public var color: UIColor = .red

    
    public init() {
        let line = SegmentLineIndicatorView()
        super.init(indicators: [line])
    }
    
    override
    public func attach(_ maker: ([SegmentIndicatorProtocol])->Void) {
        indicators.forEach { indicator in
            indicator.dataSource = self
            indicator.backgroundColor = color
            indicator.layer.cornerRadius = 0.5 * height
            indicator.layer.masksToBounds = true
        }
        maker(indicators)
    }

    func refreshSelectedIndicator(context: SegmentSelectedTransitionContext) {
        let toFrame = context.toItemFrame
        
        let w: CGFloat
        let h: CGFloat = self.height
       
        if width.isNaN {
            // 自动计算宽度
            w = context.toItemContentSize().width + padding
        } else {
            w = width + padding
        }
        
        let x: CGFloat = (toFrame.width - w) * 0.5 + toFrame.minX
        let y: CGFloat
        switch position {
        case .top:
            y = 0
        case .bottom:
            y = toFrame.minY + toFrame.height - h
        case .center:
            y = toFrame.midY - h * 0.5
        }
        
        indicators.forEach { indicator in
            indicator.frame = CGRect(x: x, y: y, width: w, height: h)
        }
    }
    
    private var updatePercent = 0
    
    func refreshScrollingIndicator(context: SegmentScrollingTransitionContext) {
        let toFrame = context.toItemFrame
        let fromFrame = context.fromItemFrame
        
        let w: CGFloat
        let h: CGFloat = self.height
       
        if width.isNaN {
            // 自动计算宽度
            w = Interpolate.interpolate(from: context.fromContentSize().width, to: context.toItemContentSize().width, percent: context.percent) + padding
        } else {
            w = width + padding
        }
        let x: CGFloat
        if context.from > context.to {
            let midX: CGFloat = Interpolate.interpolate(from: toFrame.midX, to: fromFrame.midX, percent: 1.0 - context.percent)
            x = midX - w * 0.5
        } else {
            let midX: CGFloat = Interpolate.interpolate(from: fromFrame.midX, to: toFrame.midX, percent: context.percent)
            x = midX - w * 0.5
        }
        let y: CGFloat
        switch position {
        case .top:
            y = 0
        case .bottom:
            y = toFrame.minY + toFrame.height - h
        case .center:
            y = toFrame.midY - h * 0.5
        }
        
        indicators.forEach { indicator in
            indicator.frame = CGRect(x: x, y: y, width: w, height: h)
        }
    }
}
